package com.caidan.security.config;


import com.caidan.security.web.UmsSysUserDetailsService;
import com.caidan.security.web.filter.JwtAuthenticationFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.ProviderManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
import org.springframework.security.config.annotation.authentication.configurers.userdetails.DaoAuthenticationConfigurer;
import org.springframework.security.config.annotation.method.configuration.EnableMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.provisioning.InMemoryUserDetailsManager;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import org.springframework.web.bind.annotation.CrossOrigin;

@Configuration
// 这个注解的作用是标记一个Security类 启用SpringSecurity的自定义配置
@EnableWebSecurity
@EnableMethodSecurity
public class SecurityConfig {
    /*
     * 基本配置讲解
     * */
    // 自定义用户名和密码 初始
//    一般替换成 自定义userDetailsService
//    @Bean
//    public UserDetailsService userDetailsService() {
//        // 密 码 : 1234
//        UserDetails build = User.withUsername("zhangsan")
//                .password("$2a$10$L5xB2VxwKM2kQL4SMTBdyej8e4VpfeMP3XF1660weV0n.WGRbsXdC")
//                .roles("admin" ).build();
//        UserDetails build1 = User.withUsername("user")
//                .password("$2a$10$L5xB2VxwKM2kQL4SMTBdyej8e4VpfeMP3XF1660weV0n.WGRbsXdC")
//                .roles("user" ).build();
//        InMemoryUserDetailsManager inMemoryUserDetailsManager = new InMemoryUserDetailsManager();
//        inMemoryUserDetailsManager.createUser(build);
//        inMemoryUserDetailsManager.createUser(build1);
//        return inMemoryUserDetailsManager;
//    }

    // 过滤器链
//    @Bean
//    public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
//        // 关闭csrf机制
//        http.csrf(csrf->csrf.disable());
//        // 所有人随意访问 - 核心科技
//        // 在user下面的所有路径下 都不进行验证 其他的页面进行权限认证
//        http.authorizeHttpRequests(auth->auth.requestMatchers("/user/**").permitAll().anyRequest().authenticated());
//        return http.build();
//    }


//    @Bean
//    public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
//        // 关闭csrf机制
//        http.csrf(csrf -> csrf.disable());
//        // 所有人随意访问 - 核心科技
//        // 在user下面的所有路径下 都不进行验证 其他的页面进行权限认证
//        // 使用hasRole判断 请求的是什么角色
//        http.authorizeHttpRequests(auth ->
//                auth.requestMatchers("/user/**").permitAll()
//                        .anyRequest().authenticated());
//        return http.build();
//    }

//    @Bean
//    public PasswordEncoder passwordEncoder() {
//        // 构建密码编码器
//        return new BCryptPasswordEncoder();
//    }


    /*
     * 动态权限讲解
     * 第二讲
     * */

    @Autowired
    private JwtAuthenticationFilter jwtAuthenticationFilter;
    @Bean
    public PasswordEncoder passwordEncoder() {
        // 构建密码编码器
        return new BCryptPasswordEncoder();
    }

    @Autowired
    private UmsSysUserDetailsService umsSysUserDetailsService;
    // 配置容器的基本内容
    // AuthenticationManager负责认证的
    // DaoAuthenticationProvider负责融合一些基本的内容 然后送到AuthenticationManager中

    @Bean
    public AuthenticationManager authenticationManager(PasswordEncoder passwordEncoder) {
        DaoAuthenticationProvider provider = new DaoAuthenticationProvider();

        // 关联 自定义的Details
        provider.setUserDetailsService(umsSysUserDetailsService);

        // 传递密码解析器
        provider.setPasswordEncoder(passwordEncoder);

        ProviderManager providerManager = new ProviderManager(provider);
        return providerManager;
    }

    @Bean
    public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
        http.csrf(csrf -> csrf.disable());
//        放行接口
        http.authorizeHttpRequests(auth -> auth.requestMatchers("/auth/**").permitAll()
                .anyRequest().authenticated());
        /*
        *   添加自定义过滤器链到 security中
        *   将过滤器添加上
        *   UsernamePasswordAuthenticationFilter之前执行
        * */
        http.addFilterBefore(jwtAuthenticationFilter, UsernamePasswordAuthenticationFilter.class);
        return http.build();
    }
}